package com.ec.system.modules.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.StrFormatter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.core.common.core.base.BasePageVO;
import com.ec.core.common.enums.GlobalResponseResultEnum;
import com.ec.core.common.utils.BeanUtilEx;
import com.ec.core.common.utils.CoreStringPool;
import com.ec.core.log.annotations.OperLog;
import com.ec.core.mybatis.groups.AddGroup;
import com.ec.core.mybatis.groups.UpdateGroup;
import com.ec.core.secure.annotations.PreAuth;
import com.ec.system.modules.sys.dto.QueryDictTypeDTO;
import com.ec.system.modules.sys.dto.SysDictTypeDTO;
import com.ec.system.modules.sys.entity.SysDictType;
import com.ec.system.modules.sys.service.ISysDictTypeService;
import com.ec.system.modules.sys.vo.SysDictTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 字典 前端控制器
 * </p>
 *
 * @author Eric
 * @since 2022-06-02
 */
@RestController
@RequestMapping("/dict-type")
public class SysDictTypeController {

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 查询字典
     *
     */
    @GetMapping("/{dictTypeCode}")
    @PreAuth("sys_dict_view")
    public Result<SysDictTypeVO> queryDictType(@PathVariable("dictTypeCode") String dictTypeCode) {
        SysDictType sysDictType = sysDictTypeService.getOne(new LambdaQueryWrapper<SysDictType>().eq(SysDictType::getDictTypeCode, dictTypeCode));
        if (sysDictType != null) {
            SysDictTypeVO sysDictTypeVO = BeanUtilEx.copyProperties(sysDictType, SysDictTypeVO.class);
            sysDictTypeVO.setDictTypeId(sysDictType.getDictTypeId()+"");
            return ResponseMsg.success(sysDictTypeVO);
        }
        return ResponseMsg.success();
    }

    /**
     * 字典列表
     *
     * @param queryDictTypeDTO 查询字典类型DTO
     * @return com.ec.core.common.core.api.Result<com.ec.core.common.core.base.BasePageVO <com.ec.system.modules.sys.vo.SysDictTypeVO>>
     * @author Eric Lee
     * @date 2022/6/2 16:59
     */
    @GetMapping("list")
    @PreAuth("sys_dict_view")
    public Result<BasePageVO<SysDictTypeVO>> listDictType(QueryDictTypeDTO queryDictTypeDTO) {
        return ResponseMsg.success(sysDictTypeService.listDictType(queryDictTypeDTO));
    }

    /**
     * 创建字典
     *
     * @param sysDictTypeDTO
     * @return
     * @author Eric Lee
     * @date 2022/6/2 16:59
     */
    @PostMapping
    @PreAuth("sys_dict_add")
    @OperLog(desc = "新增字典dictType")
    public Result<Void> addDictType(@Validated(AddGroup.class) @RequestBody SysDictTypeDTO sysDictTypeDTO) {
        if (sysDictTypeService.checkDictType(sysDictTypeDTO.getDictTypeCode())) {
            return ResponseMsg.error(GlobalResponseResultEnum.DEFAULT_ERROR.getCode(),
                    StrFormatter.format("【{}】已经存在", sysDictTypeDTO.getDictTypeCode()));
        }
        sysDictTypeDTO.setDictTypeId(null);
        SysDictType dictType = new SysDictType();
        BeanUtil.copyProperties(sysDictTypeDTO, dictType);
        return ResponseMsg.status(sysDictTypeService.save(dictType));
    }

    /**
     * 修改字典
     *
     * @param sysDictTypeDTO
     * @return
     * @author Eric Lee
     * @date 2022/6/2 16:59
     */
    @PutMapping
    @PreAuth("sys_dict_update")
    @OperLog(desc = "修改字典dictType")
    public Result<Void> updateDictType(@Validated(UpdateGroup.class) @RequestBody SysDictTypeDTO sysDictTypeDTO) {
        SysDictType dictType = new SysDictType();
        sysDictTypeDTO.setDictTypeCode(null);
        BeanUtil.copyProperties(sysDictTypeDTO, dictType);
        return ResponseMsg.status(sysDictTypeService.updateById(dictType));
    }

    /**
     * 删除字典
     *
     * @param dictTypeId
     * @return
     * @author Eric Lee
     * @date 2022/6/2 16:59
     */
    @DeleteMapping("/{dictTypeId}")
    @PreAuth("sys_dict_delete")
    @OperLog(desc = "删除字典dictType")
    public Result<Void> deleteDictType(@PathVariable long dictTypeId) {
        return ResponseMsg.status(sysDictTypeService.removeById(dictTypeId));
    }
}
